package com.dfbz.springboot_project.service.impl;

import com.dfbz.springboot_project.entity.AdminInfo;
import com.dfbz.springboot_project.mapper.AdminInfoMapper;
import com.dfbz.springboot_project.service.AdminInfoService;
import com.dfbz.springboot_project.service.AdminRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdminInfoServiceImpl implements AdminInfoService {
    @Autowired
    AdminInfoMapper adminInfoMapper;
    @Autowired
    AdminRoleService adminRoleService;
    @Override
    public List<AdminInfo> selectAll() {
        return adminInfoMapper.selectAll();
    }

    @Override
    public AdminInfo selectByName(String username) {
        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setAdminCode(username);
        return adminInfoMapper.selectOne(adminInfo);
    }

    @Override
    public PageInfo<AdminInfo> selectPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<AdminInfo> adminInfos = adminInfoMapper.selectAll();
        for (AdminInfo adminInfo : adminInfos) {
            List<String> roles = adminInfoMapper.selectRolesByAid(adminInfo.getAdminId());
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < roles.size(); i++) {
                sb.append(roles.get(i)+"、");
            }
            if (sb!=null&&sb.length()>0){
                sb.delete(sb.length()-1,sb.length());
            }
            adminInfo.setRoles(sb.toString());
        }
        return new PageInfo<>(adminInfos);
    }
    @Override
    public AdminInfo selectOneByAid(Integer Aid){
        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setAdminId(Aid);
        return adminInfoMapper.selectOne(adminInfo);
    }

    @Override
    public void deleteById(Integer Aid) {
        adminInfoMapper.deleteByPrimaryKey(Aid);
    }
    @Override
    public void InsertSelective(Map<String, Object> params){
        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setAdminName((String) params.get("name"));
        adminInfo.setAdminCode((String) params.get("code"));
        adminInfo.setAdminPassword((String) params.get("password"));
        adminInfo.setAdminTelephone((String) params.get("telephone"));
        adminInfo.setAdminEmail((String) params.get("email"));
        adminInfo.setAdminEnrolldate(new Date());
        adminInfoMapper.insertSelective(adminInfo);
        List<AdminInfo> adminInfos = adminInfoMapper.selectAll();
        AdminInfo adminInfo1 = adminInfos.get(adminInfos.size() - 1);
        params.put("Aid",adminInfo1.getAdminId());
        adminRoleService.updateSelective(params);
    }
    @Override
    public void update(Map<String, Object> params){
        AdminInfo adminInfo = (AdminInfo) params.get("adminInfo");
        adminInfoMapper.updateByPrimaryKey(adminInfo);
    }
}
